import { Injectable } from '@angular/core';
import { ApolloQueryResult, FetchResult } from '@apollo/client/core';
import { Apollo, gql, QueryRef } from 'apollo-angular';
import { Observable } from 'rxjs';
import { FileServiceApi,
FileService_requestPathResult,
FileService_requestPathVariables,
FileService_listFileResult,
FileService_listFileVariables,
FileService_listResult,
FileService_listVariables,
FileService_totalCountResult,
FileService_searchResult,
FileService_searchVariables,
FileService_loadAllResult,
FileService_getByIdResult,
FileService_getByIdVariables,
FileService_saveResult,
FileService_saveVariables,
FileService_removeByIdResult,
FileService_removeByIdVariables,
FileService_removeByIdListResult,
FileService_removeByIdListVariables,
MelonFileResultInfo, MelonFilePageInfo,
MelonFileConnection,
MelonFilePackage,
MelonFileOrderBy,
MelonFileMutationInfo,
MelonFileStringFilter,
MelonFileNumberFilter,
MelonFileBooleanFilter,
fileServiceQuery,
fileServiceMutation,
FileServiceQueryOptions,
MelonFileStore,
MelonFileView,
MelonFileInput,
MelonFileFilter,
MelonFileErrorBody} from '../model/file_service.model';

/************************************************************************
 * 文件管理
 */
@Injectable({
    providedIn: 'root'
})
export class FileService implements FileServiceApi {

        private requestPathGql:string;
        private listFileGql:string;
        private listGql:string;
        private totalCountGql:string;
        private searchGql:string;
        private searchObjectRef: QueryRef<FileService_searchResult>;
        private loadAllGql:string;
        private getByIdGql:string;

    constructor(private apollo: Apollo) {
        this.requestPathGql=`query nars_fileService_requestPath($path: String!){${fileServiceQuery.fileService_requestPath}}`;
        this.listFileGql=`query nars_fileService_listFile($path: String!){${fileServiceQuery.fileService_listFile}}`;
        this.listGql=`query nars_fileService_list($filter: MelonFileFilter!,$orderBy: OrderBy){${fileServiceQuery.fileService_list}}`;
        this.totalCountGql=`query nars_fileService_totalCount{${fileServiceQuery.fileService_totalCount}}`;
        this.searchGql=`query nars_fileService_search($first: Int!,$offset: Float!,$filter: MelonFileFilter!,$orderBy: OrderBy){${fileServiceQuery.fileService_search}}`;
        this.searchObjectRef = apollo.watchQuery<FileService_searchResult>({ query: gql`${this.searchGql}`, fetchPolicy:'cache-first' });
        this.loadAllGql=`query nars_fileService_loadAll{${fileServiceQuery.fileService_loadAll}}`;
        this.getByIdGql=`query nars_fileService_getById($id: String!){${fileServiceQuery.fileService_getById}}`;
    }
    /**
     * 获取读取路径
     */
    requestPath(variables: FileService_requestPathVariables, options:FileServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<FileService_requestPathResult>> {
            return this.apollo.query({ query: gql`${this.requestPathGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 获取文件列表
     */
    listFile(variables: FileService_listFileVariables, options:FileServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<FileService_listFileResult>> {
            return this.apollo.query({ query: gql`${this.listFileGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 分页查询
     */
    list(variables: FileService_listVariables, options:FileServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<FileService_listResult>> {
            return this.apollo.query({ query: gql`${this.listGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 统计库中所有对象的数据量
     */
    totalCount(options:FileServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<FileService_totalCountResult>> {
            return this.apollo.query({ query: gql`${this.totalCountGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 游标查询
     */
    search(variables: FileService_searchVariables, options:FileServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<FileService_searchResult>> {
        this.searchObjectRef.setVariables(variables);
        if(options.more){
            this.searchObjectRef.fetchMore({variables});
        }
        return this.searchObjectRef.valueChanges;
    }
    /**
     * 获取库中所有数据，数据量大时请谨慎使用
     */
    loadAll(options:FileServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<FileService_loadAllResult>> {
            return this.apollo.query({ query: gql`${this.loadAllGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 获取一个对象
     */
    getById(variables: FileService_getByIdVariables, options:FileServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<FileService_getByIdResult>> {
            return this.apollo.query({ query: gql`${this.getByIdGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 保存对象
     */
    save(variables: FileService_saveVariables): Observable<FetchResult<FileService_saveResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_fileService_save($entity: MelonFileInput!){${fileServiceMutation.fileService_save}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一个对象
     */
    removeById(variables: FileService_removeByIdVariables): Observable<FetchResult<FileService_removeByIdResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_fileService_removeById($id: String!){${fileServiceMutation.fileService_removeById}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一批对象
     */
    removeByIdList(variables: FileService_removeByIdListVariables): Observable<FetchResult<FileService_removeByIdListResult>>{
        return this.apollo.mutate({mutation: gql`mutation nars_fileService_removeByIdList($ids: [String]!){${fileServiceMutation.fileService_removeByIdList}}`, variables,fetchPolicy:'no-cache'});
    }









}
